var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showPopover: false,
    actions: [
      { text: "商品名" , value: "goods_title"}, 
      { text: "订单号" , value: "order_sn"},
      { text: "会员信息" , value: "member_kwd"},
    ],
    search_column: { text: "会员信息" , value: "member_kwd"}, 
    loading: false,
    finished: false,
    page: 1,
    dataList: [],
    keyword: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id: options.id
    })
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getData("loading");
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  btnPopover() {
    this.setData({
      showPopover: !this.data.showPopover
    })
  },
  onSelect(item) {
    this.setData({
      search_column: item.currentTarget.dataset.item,
      showPopover: false,
      dataList: [],
      page: 1,
      total: 0,
      finished: false,
    })
    this.getData();
  },
  inpText(e) {
    this.setData({
      keyword: e.detail.value,
      dataList: [],
      page: 1,
      total: 0,
      finished: false,
    });
    this.getData();
  },
  getData(val) {
    if (this.data.finished) return;
    app._getNetWork({
      url: app.getNetAddresss("plugin.region-external-reward.frontend.controllers.client.client.childOrderList"),
      data: {
        page: this.data.page,
        child_id: this.data.id,
        keyword: this.data.keyword,
        search_column: this.data.search_column.value
      },
      success: (respone) => {
        if (respone.data.result !== 1) {
          wx.showToast({
            title: respone.data.msg,
          });
          return false;
        }
        let res = respone.data.data;
        let finished = false;
        let dataList = [];
        let total = 1;
        if(val == "loading") {
          dataList = this.data.dataList.concat(res.list.data);
        }else {
          dataList = res.list.data;
        }
        for(let item of dataList) {
          if(item.order_goods.length) {
            item.total = 0;
            for(let citem of item.order_goods) {
              item.total = item.total + citem.total
            }
          }
        }
        if (res.list.data.length === 0 || res.list.data.length < res.list.per_page || res.list.current_page === res.list.last_page) {
          finished = true;
        }
        total = res.total;
        this.setData({
          finished,
          dataList,
          page: ++this.data.page,
          total,
        });
      }
    });
  },
})